<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Reg_validation {

    function validate($u, $p, $n, $a, $g, $tl, $tm, $nh) {
        if ($u != "" && $p != "" && $n != "" && $a != "" && $g != "" && $tl != "" && $tm != "" && $nh != "") {
            //cek input
            //jika semuanya valid, maka return true
            if ($this->cek_form_input('username', $u)
                    && $this->cek_form_input('password', $p)
                    && $this->cek_form_input('nama', $n)
                    && $this->cek_form_input('alamat', $a)
                    && $this->cek_form_input('gaji', $g)
                    && $this->cek_form_input('tgllahir', $tl)
                    && $this->cek_form_input('tglmasuk', $tm)
                    && $this->cek_form_input('nohp', $nh) ) {
                return true;
            }
            else
                return false;
        }
        else {
            return false;
        }
    }

    function cek_form_input($column, $value) {


        if ($column == 'username') {
            if (strlen($value) <= 100) {
                return true;
            }
            else
                return false;
        }
        else if( $column == 'password'){
            if (strlen($value) <= 100) {
                return true;
            }
            else
                return false;
        }
        else if ($column == 'nama') {
            if (strlen($value) <= 60) {
                return true;
            }
            else
                return false;
        }
        else if ($column == 'alamat') {
            if (strlen($value) <= 400) {
                return true;
            }
            else
                return false;
        }
        else if ($column == 'gaji') {

            return true;
        } else if ($column == 'tgllahir') {
            if ($this->is_valid_date($value)) {
                return true;
            }
            else
                return false;
        }
        else if ($column == 'tglmasuk') {
            if ($this->is_valid_date($value)) {
                return true;
            }
            else
                return false;
        }
        else if ($column == 'nohp') {
            if (strlen($column) <= 13) {
                return true;
            }
            else
                return false;
        }
        else
            return false;
    }

    function is_valid_date($input) {
        $valid_year = false;
        $valid_month = false;
        $valid_day = false;
        $exp = explode("-", $input);
        if (count($exp) == 3) {


            $valid_year = true;


            if ($exp[1] > 0 && $exp[1] <= 13) {
                $valid_month = true;
            }


            if ($exp[1] == "1" || $exp[1] == "3" || $exp[1] == "5" || $exp[1] == "7" || $exp[1] == "8" || $exp[1] == "10" || $exp[1] == "12") {
                if ($exp[2] > 0 && $exp[2] <= 31) {
                    $valid_day = true;
                }
            } else if ($exp[1] == "4" || $exp[1] == "6" || $exp[1] == "8" || $exp[1] == "10") {
                if ($exp[2] > 0 && $exp[2] <= 30) {
                    $valid_day = true;
                }
            } else if ($exp[1] == "2") {
                if ($this->is_kabisat($exp[0])) {
                    if ($exp[2] > 0 && $exp[2] <= 29) {
                        $valid_day = true;
                    }
                } else {
                    if ($exp[2] > 0 && $exp[2] <= 28) {
                        $valid_day = true;
                    }
                }
            }
        }
        if ($valid_year && $valid_month && $valid_day) {
            return true;
        } else {
            return false;
        }
    }

    function is_kabisat($year) {
        if ($year % 4 == 0) {
            return true;
        } else {
            return false;
        }
    }

}

?>
